<?php
namespace Admin\Controller;
use Think\Controller;
class BookController extends Controller {
    public function index(){

		$book_name = I('post.book_name','');
		$where =array();
		if(!empty($book_name))
		{
			$where['book_name'] = array('like',$book_name);
		}
		
		$Model 		= new \Think\Model\MongoModel('book');
		$count      = $Model->where($where)->count();// 查询满足要求的总记录数
		include MODULE_PATH.'Common/'.'Page.class.php';
		$Page       = new \Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$book_list = $Model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('book_list',$book_list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		//取书籍类型
		$type_list = D('BookType')->get_list();
		$this->assign('type_list',$type_list);
		$this->display('book_list');
    }
	
	//图书列表
	public function book_list()
	{	
		$type_name = I('post.type_name','');
		$where =array();
		if(!empty($type_name))
		{
			$where['type_name'] = "$type_name";
		}
		
		$Model 		= new \Think\Model\MongoModel('book');
		$count      = $Model->where($where)->count();// 查询满足要求的总记录数
		include MODULE_PATH.'Common/'.'Page.class.php';
		$Page       = new \Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$book_list = $Model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('book_list',$book_list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		//取书籍类型
		$type_list = D('BookType')->get_list();
		$this->assign('type_list',$type_list);
		$this->display();
	}
	
	//添加图书
	public function add_book()
	{
		$submit = I('post.submit','');
		
		
		if(!empty($submit))
		{
			$book_name = I('post.book_name','');
			if(!empty($book_name ))
			{
				$author = I('post.author','');
				$datetime = date('Y-m-d H:i:s',time());
				$status = I('post.status','1');
				$type_name = I('post.type_name','');
				$thumbnail = I('post.thumbnail','');
				
				$data = array();
				$data['book_name'] 	= $book_name;
				$data['author'] 	= $author;
				$data['type_name'] 	= $type_name;
				$data['status'] 	= $status;
				$data['thumbnail'] 	= $thumbnail;
				$data['datetime'] 	= $datetime;
				$res = D('book')->add_book($data);
				
				if ($res['ok']) {
					$this->success('添加成功',__ROOT__.'/index.php/Admin/Book/book_list');
				}else {
					$this->error('添加失败');
				}
			}else{
				$this->error('书名不能为空');
			}
			
		}else {
			//取书籍类型
			$type_list = D('BookType')->get_list();
			$this->assign('type_list',$type_list);
			$this->display();
		}
		
	}
	//修改图书
	public function edit_book()
	{
		$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍');
		}else
		{
			$submit = I('post.submit','');
			
			if (!empty($submit)) {
				
				$book_name = I('post.book_name','');
				$author = I('post.author','');
				$datetime = date('Y-m-d H:i:s',time());
				$status = I('post.status','1');
				$type_name = I('post.type_name','');
				$thumbnail = I('post.thumbnail','');
				
				$data = array();
				$data['book_name'] = $book_name;
				$data['author'] = $author;
				$data['type_name'] = $type_name;
				$data['status'] = $status;
				$data['thumbnail'] = $thumbnail;
				$data['datetime'] = $datetime;
				$res = D('book')->edit_book($id,$data);
				if ($res['ok']) {
					$this->success('修改成功');
				}else {
					$this->error('修改失败');
				}
			}else {
				//取书籍类型
				$type_list = D('BookType')->get_list();
				$this->assign('type_list',$type_list);
				//查找书
				$book = D('book')->find_book($id);
				$this->assign('book',$book[$id]);
				$this->assign('book_id',$id);
				$this->display();
			}
			
		}
		
	}
	//删除图书
	public function del_book()
	{
		//
		$id = I('get.id','');
		if(empty($id))
		{
			$this->error('没有找到此书籍');
		}else
		{
			$res = D('book')->del_book($id);
			if ($res) {
				$this->success('删除成功');
			}else {
				$this->error('删除失败');
			}
		}
	
	}
	
}